<template>
  <view class="work-container">
    <!-- 轮播图 -->
    <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
      <swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
        <swiper-item v-for="(item, index) in data" :key="index">
          <view class="swiper-item" @click="clickBannerItem(item)">
            <image :src="item.image" mode="aspectFill" :draggable="false" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot>

    <!-- 宫格组件 -->
    <uni-section title="系统管理" type="line"></uni-section>
    <view class="grid-body">
      <uni-grid :column="4" :showBorder="false">
        <uni-grid-item>
          <view class="grid-item-box" @click="handleItemClick">
            <uni-icons type="person-filled" size="30"></uni-icons>
            <text class="text">测试工具</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box" @click="showChat">
            <uni-icons type="chat-filled" size="30"></uni-icons>
            <text class="text">聊天</text>
          </view>
        </uni-grid-item>
		<uni-grid-item>
		  <view class="grid-item-box" @click="showData">
		    <uni-icons type="staff-filled" size="30"></uni-icons>
		    <text class="text">查看服务器数据</text>
		  </view>
		</uni-grid-item>
		<uni-grid-item>
		  <view class="grid-item-box" @click="showEsp32">
		    <uni-icons type="staff-filled" size="30"></uni-icons>
		    <text class="text">ESP32控制</text>
		  </view>
		</uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="staff-filled" size="30"></uni-icons>
            <text class="text">角色管理</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="color" size="30"></uni-icons>
            <text class="text">菜单管理</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="settings-filled" size="30"></uni-icons>
            <text class="text">部门管理</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="heart-filled" size="30"></uni-icons>
            <text class="text">岗位管理</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="bars" size="30"></uni-icons>
            <text class="text">字典管理</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="gear-filled" size="30"></uni-icons>
            <text class="text">参数设置</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box">
            <uni-icons type="chat-filled" size="30"></uni-icons>
            <text class="text">通知公告</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box" @click="showJob">
            <uni-icons type="wallet-filled" size="30"></uni-icons>
            <text class="text">定时任务</text>
          </view>
        </uni-grid-item>
        <!-- 添加新的待办(H5)功能项 -->
        <uni-grid-item>
          <view class="grid-item-box" @click="openTodoWeb">
            <uni-icons type="paperplane-filled" size="30"></uni-icons>
            <text class="text">待办(H5)</text>
          </view>
        </uni-grid-item>
        <!-- 添加新的外部系统WebView功能项 -->
        <uni-grid-item>
          <view class="grid-item-box" @click="openExternalSystem">
            <uni-icons type="cloud-download-filled" size="30"></uni-icons>
            <text class="text">外部系统</text>
          </view>
        </uni-grid-item>
      </uni-grid>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        current: 0,
        swiperDotIndex: 0,
        isNavigating: false, // 防止重复点击的标志位
        data: [{
            image: '/static/images/banner/banner01.jpg'
          },
          {
            image: '/static/images/banner/banner02.jpg'
          },
          {
            image: '/static/images/banner/banner03.jpg'
          }
        ]
      }
    },
    methods: {
      clickBannerItem(item) {
        console.info(item)
      },
      changeSwiper(e) {
        this.current = e.detail.current
      },
      changeGrid(e) {
		console.log("e=",e)
        this.$modal.showToast('模块建设中~')
      },
	  // 打开H5待办页面
	  openTodoWeb() {
	    // 直接在App内部打开webview页面
	    uni.navigateTo({
	      url: '/pages/work/mytool/todo/webview'
	    })
	  },
	  
	  // 打开外部系统页面
	  openExternalSystem() {
	    // 直接在App内部打开webview页面
	    uni.navigateTo({
	      url: '/pages/work/mytool/webview/index'
	    })
	  },
	   handleItemClick() {
		   // 在这里处理点击事件
		   console.log('Grid item clicked');
		   uni.navigateTo({
		           url: '/pages/work/mytool/test/index'
		  });
	       
		},
		showData(){
		// 在这里处理点击事件
			 console.log('showData clicked');
			 uni.navigateTo({
					 url: '/pages/work/mytool/ipscan/index'
			});
		},
		
		showEsp32(){
		// 在这里处理点击事件
			 console.log('showEsp32 clicked');
			 uni.navigateTo({
					 url: '/pages/work/mytool/esp32/index'
			});
		},
		
		showChat() {
			// 处理聊天点击事件
			console.log('showChat clicked');
			
			// 防止重复点击
			if (this.isNavigating) {
				console.log('正在跳转中，请稍候...');
				uni.showToast({
					title: '正在跳转中...',
					icon: 'loading',
					duration: 1000
				});
				return;
			}
			
			this.isNavigating = true;
			
			// 显示加载提示
			uni.showToast({
				title: '正在进入聊天...',
				icon: 'loading',
				duration: 1500
			});
			
			// 检查是否在HBuilderX内置浏览器中运行
			// #ifdef H5
			if (process.env.NODE_ENV === 'development') {
				console.log('开发模式：准备跳转到聊天页面');
			}
			// #endif
			
			uni.navigateTo({
				url: '/pages/work/mytool/chat/index',
				success: (res) => {
					console.log('跳转聊天页面成功', res);
					this.isNavigating = false;
					uni.hideToast(); // 隐藏加载提示
				},
				fail: (err) => {
					console.error('跳转聊天页面失败', err);
					this.isNavigating = false;
					uni.hideToast(); // 隐藏加载提示
					
					// 失败时尝试使用redirectTo
					console.log('尝试使用redirectTo跳转...');
					uni.redirectTo({
						url: '/pages/work/mytool/chat/index',
						success: () => {
							console.log('redirectTo跳转成功');
						},
						fail: (redirectErr) => {
							console.error('redirectTo也失败了', redirectErr);
							// 显示错误提示
							uni.showToast({
								title: '页面跳转失败，请重试',
								icon: 'error',
								duration: 2000
							});
						}
					});
				},
				complete: () => {
					// 无论成功失败，2秒后重置状态
					setTimeout(() => {
						this.isNavigating = false;
					}, 2000);
				}
			});
		},
		
		showJob() {
			// 处理定时任务点击事件
			console.log('showJob clicked');
			uni.navigateTo({
				url: '/pages/work/mytool/job/index'
			});
		},
		
		showTodo() {
			// 处理待办事项点击事件
			console.log('showTodo clicked');
			uni.navigateTo({
				url: '/pages/work/mytool/todo/index'
			});
		}
    }
  }
</script>

<style lang="scss">
  /* #ifndef APP-NVUE */
  page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background-color: #fff;
    min-height: 100%;
    height: auto;
  }

  view {
    font-size: 14px;
    line-height: inherit;
  }

  /* #endif */

  .text {
    text-align: center;
    font-size: 26rpx;
    margin-top: 10rpx;
  }

  .grid-item-box {
    flex: 1;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 15px 0;
  }

  .uni-margin-wrap {
    width: 690rpx;
    width: 100%;
    ;
  }

  .swiper {
    height: 300rpx;
  }

  .swiper-box {
    height: 150px;
  }

  .swiper-item {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    height: 300rpx;
    line-height: 300rpx;
  }

  @media screen and (min-width: 500px) {
    .uni-swiper-dot-box {
      width: 400px;
      /* #ifndef APP-NVUE */
      margin: 0 auto;
      /* #endif */
      margin-top: 8px;
    }

    .image {
      width: 100%;
    }
  }
</style>